package cc.sjfx.sharedoc.widget.adapter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.listener.UpdateListener;
import cn.mandroid.frame.toast.MToast;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.activity.BasicFragmentActivity;
import cc.sjfx.sharedoc.activity.examination.DetailActivity;
import cc.sjfx.sharedoc.activity.user.UserLoginActivity;
import cc.sjfx.sharedoc.bean.ExamShowListViewBean;
import cc.sjfx.sharedoc.cache.UserCache;
import cc.sjfx.sharedoc.db.UserDB;
import cc.sjfx.sharedoc.db.UserDB.WebDataCallBack;
import cc.sjfx.sharedoc.db.bmob.User;
import cc.sjfx.sharedoc.function.AsyncImageLoader;
import cc.sjfx.sharedoc.function.AsyncImageLoader.ImageCallback;
import cc.sjfx.sharedoc.utils.CheckUtils;
import cc.sjfx.sharedoc.widget.view.ButtonWithNumber;
import cc.sjfx.sharedoc.widget.view.xlistview.XListView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class QuestionXListViewAdapter extends BaseAdapter {
	private Context context;
	private List<ExamShowListViewBean> list = new ArrayList<ExamShowListViewBean>();
	ViewHolder holder = null;
	AsyncImageLoader loader;
	View view = null;

	public QuestionXListViewAdapter(Context context,
			List<ExamShowListViewBean> list, XListView xListView) {
		this.context = context;
		this.list = list;
		loader = new AsyncImageLoader(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	private class ViewHolder {
		public TextView nickText;
		public TextView titleText;
		public TextView contenText;
		public ImageView starImg;
		public ButtonWithNumber buttonWithNumber;
		public ImageView userIcoImg;
		public TextView createTimeText;
		public boolean isCollected = false;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		holder = null;
		final ExamShowListViewBean bean = list.get(arg0);
		if (arg1 == null) {
			holder = new ViewHolder();
			arg1 = LayoutInflater.from(context).inflate(
					R.layout.xlistview_question, null);
			view = arg1;
			holder.buttonWithNumber = (ButtonWithNumber) view
					.findViewById(R.id.button2);
			holder.contenText = (TextView) view.findViewById(R.id.contentText);
			holder.nickText = (TextView) view.findViewById(R.id.nickText);
			holder.starImg = (ImageView) view.findViewById(R.id.starImg1);
			if (holder.isCollected) {
				holder.starImg.setImageResource(R.drawable.star_collected);
			}
			holder.titleText = (TextView) view.findViewById(R.id.titleText);
			holder.userIcoImg = (ImageView) view.findViewById(R.id.icoImg);
			holder.createTimeText = (TextView) view
					.findViewById(R.id.createTimeText);
			view.setTag(holder);

		} else {
			holder = (ViewHolder) arg1.getTag();
		}
		if (!UserCache.isLogin || UserCache.user.getCollectList() == null) {
			holder.isCollected = false;
		} else {
			holder.isCollected = UserCache.user.getCollectList().contains(
					bean.getObjectId());
		}

		holder.titleText.setText(bean.getTitle());
		if (CheckUtils.stringIsNull(bean.getUser().ico)) {
			UserDB.getUserData(context, bean.getUser().username,
					new WebDataCallBack() {

						@Override
						public void getIco(String img) {
							// TODO Auto-generated method stub
							loader.loadDrawable(img, new ImageCallback() {
								@Override
								public void imageLoaded(Bitmap bitmap) {
									holder.userIcoImg
											.setImageBitmap(bitmap);
									notifyDataSetChanged();
								}
							});
						}
					});
		} else {
			loader.loadDrawable(bean.getUser().ico, new ImageCallback() {
				@Override
				public void imageLoaded(Bitmap bitmap) {
					holder.userIcoImg.setImageBitmap(bitmap);
				}
			});
		}
		holder.contenText.setText(bean.getContent());
		holder.nickText.setText(bean.getUser().nickName);
		holder.createTimeText.setText(bean.getCreateTime());
		holder.buttonWithNumber.setCount(bean.getReplyNum());
		arg1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putSerializable("info", bean);
				bundle.putInt("type", 2);
				((BasicFragmentActivity) context).startActivity(bundle,
						DetailActivity.class);
			}
		});
		holder.nickText.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});
		if (holder.isCollected) {
			holder.starImg.setImageResource(R.drawable.star_collected);
		} else {
			holder.starImg.setImageResource(R.drawable.star);
		}
		holder.starImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (UserCache.isLogin) {
					boolean iscollect = UserCache.user.getCollectList()
							.contains(bean.getObjectId());
					doCollection(bean.getObjectId(), holder.starImg, iscollect);
				} else {
					((BasicFragmentActivity) context).startActivity(null,
							UserLoginActivity.class);
				}
			}
		});
		return arg1;
	}

	protected void doCollection(String objectId, final ImageView starImg,
			final boolean isCollected) {
		// TODO Auto-generated method stub
		List<String> list = UserCache.user.getCollectList();
		if (isCollected) {
			list.remove(objectId);
		} else {
			list.add(objectId);
		}
		User user = new User();
		user.setCollectList(list);
		user.update(context, UserCache.user.getObjectId(),
				new UpdateListener() {
					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						if (isCollected) {

							MToast.show(context, "已取消");
							starImg.setImageResource(R.drawable.star);
						} else {
							MToast.show(context, "已收藏");
							starImg.setImageResource(R.drawable.star_collected);
						}
						notifyDataSetChanged();
					}

					@Override
					public void onFailure(int arg0, String arg1) {
						// TODO Auto-generated method stub
						MToast.show(context, "收藏失败，请稍后再试");
					}
				});
	}
}
